/**
* @program: Go
*
* @description:websocket的后端服务
*
* @author: Mr.chen
*
* @create: 2020-11-01 17:34
**/
package rpc

import (
	"ydd-im/service/api/define"
	"ydd-im/service/api/services/wsservice/client"
	"ydd-im/service/api/services/wsservice/pb"
	"ydd-im/service/api/tools/response"


	//"ydd-im/service/api/tools/util"
	"ydd-im/util/logger"
	"go.uber.org/zap"

	//"ydd-im/service/api/tools/util"
	"context"
	//log "github.com/sirupsen/logrus"
	"google.golang.org/grpc"
	"net"
)

type CommonServiceServer struct{}

func (this *CommonServiceServer) SendToUser(ctx context.Context, req *pb.SendToUserReq) (*pb.SendToUserReply, error) {
	logger.GetLg().Info("接收到RPC指定客户端消息",
		zap.String("host",define.LocalHost),
		zap.String("port",define.Port),
		zap.String("clientId",req.ClientId),
	)
	messageData := response.MessageData{
		MessageLogId:int(req.Data.MessageLogId),
		ToUserId:int(req.Data.ToUserId),
		Media:int(req.Data.Media),
		Pic:req.Data.Pic,
		Url:req.Data.Url,
		MessageType:int(req.Data.MessageType),
		SendTime:req.Data.SendTime,
		Content:req.Data.Content,
		Dstid:int(req.Data.Dstid),
	}
	client.SendMessageLocalUser(req.MessageId, req.ClientId,req.SendUserId, int(req.Code), req.Message, messageData)
	return &pb.SendToUserReply{}, nil
}

func (this *CommonServiceServer) CloseClient(ctx context.Context, req *pb.CloseClientReq) (*pb.CloseClientReply, error) {

	return &pb.CloseClientReply{}, nil
}

//添加分组到group
func (this *CommonServiceServer) BindGroup(ctx context.Context, req *pb.BindGroupReq) (*pb.BindGroupReply, error) {

	return &pb.BindGroupReply{}, nil
}

func (this *CommonServiceServer) Send2Group(ctx context.Context, req *pb.Send2GroupReq) (*pb.Send2GroupReply, error) {

	return &pb.Send2GroupReply{}, nil
}

func (this *CommonServiceServer) Send2System(ctx context.Context, req *pb.Send2SystemReq) (*pb.Send2SystemReply, error) {

	return &pb.Send2SystemReply{}, nil
}

//获取分组在线用户列表
func (this *CommonServiceServer) GetGroupClients(ctx context.Context, req *pb.GetGroupClientsReq) (*pb.GetGroupClientsReply, error) {

	return &pb.GetGroupClientsReply{}, nil
}



func InitGRpcServer(port string) {
	define.RPCPort = port
	go createGRPCServer(":" + port)
}


func createGRPCServer(port string) {
	lis, err := net.Listen("tcp", port)
	if err != nil {
		panic(err)
	}
	s := grpc.NewServer()

	pb.RegisterUserServiceServer(s,&UserServiceServer{})
	pb.RegisterCommonServiceServer(s, &CommonServiceServer{})
	pb.RegisterDBProxyServiceServer(s,&DBProxyServiceServer{})
	err = s.Serve(lis)
	if err != nil {
		panic(err)
	}
}